package IT.DataAccess;

import IT.Entities.ClienteTelefono;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatClienteTelefono implements IDatClienteTelefono{
    
    private List<ClienteTelefono> oLista;
    private ClienteTelefono oTelefono;
    private BDConexion oBase;    
    private Connection oConexion;
    private CallableStatement oCst;    
    private ResultSet oRst;    
    
    public List<ClienteTelefono> Listar() {
       oLista=new ArrayList<ClienteTelefono>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteTelefonoListar()}");
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new ClienteTelefono(oRst.getInt("cod_tel_in"), 
                        oRst.getInt("tip_tel_in"), oRst.getString("Tipo"), 
                        oRst.getString("num_tel_vc"), oRst.getString("anx_tel_vc"), 
                        oRst.getBoolean("prin_tel_bo"), oRst.getString("Principal"),
                        oRst.getInt("est_tel_in"), 
                        oRst.getString("Estado"), oRst.getInt("cod_cli_in")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public ClienteTelefono Ind(int nCodigo) {
       oTelefono=new ClienteTelefono();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteTelefonoInd(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oTelefono.setCod_tel_in(oRst.getInt("cod_tel_in"));
                oTelefono.setTip_tel_in(oRst.getInt("tip_tel_in"));
                oTelefono.setTipo(oRst.getString("Tipo"));
                oTelefono.setNum_tel_in(oRst.getString("num_tel_vc"));
                oTelefono.setAnx_tel_vc(oRst.getString("anx_tel_vc"));
                oTelefono.setPrin_tel_bo(oRst.getBoolean("prin_tel_bo"));
                oTelefono.setPrincipal(oRst.getString("Principal"));
                oTelefono.setEst_tel_in(oRst.getInt("est_tel_in"));
                oTelefono.setEstado( oRst.getString("Estado"));
                oTelefono.setCod_cli_in(oRst.getInt("cod_cli_in"));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oTelefono;
    }

    public List<ClienteTelefono> Ubi(int nCliente) {
        oLista=new ArrayList<ClienteTelefono>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteTelefonoUbi(?)}");
            oCst.setInt(1, nCliente);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new ClienteTelefono(oRst.getInt("cod_tel_in"), 
                        oRst.getInt("tip_tel_in"), oRst.getString("Tipo"), 
                        oRst.getString("num_tel_vc"), oRst.getString("anx_tel_vc"), 
                        oRst.getBoolean("prin_tel_bo"),oRst.getString("Principal"),
                        oRst.getInt("est_tel_in"),  oRst.getString("Estado"), 
                        oRst.getInt("cod_cli_in")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return oLista;
    }

    public void Insertar(ClienteTelefono ent) {
       try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteTelefonoInsertar(?,?,?, ?,?,?)}");              
                                   
            oCst.setInt(1, ent.getTip_tel_in());
            oCst.setString(2, ent.getNum_tel_in());
            oCst.setString(3, ent.getAnx_tel_vc());
            oCst.setBoolean(4, ent.isPrin_tel_bo());
            oCst.setInt(5, ent.getEst_tel_in());
            oCst.setInt(6, ent.getCod_cli_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(ClienteTelefono ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteTelefonoEditar(?,?,?, ?,?,?, ?)}");              
            
            oCst.setInt(1, ent.getCod_tel_in());
            oCst.setInt(2, ent.getTip_tel_in());
            oCst.setString(3, ent.getNum_tel_in());
            oCst.setString(4, ent.getAnx_tel_vc());
            oCst.setBoolean(5, ent.isPrin_tel_bo());
            oCst.setInt(6, ent.getEst_tel_in());
            oCst.setInt(7, ent.getCod_cli_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Eliminar(int nCodigo) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteTelefonoEliminar(?)}");             
                                   
            oCst.setInt(1, nCodigo);          
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int Valida(ClienteTelefono ent) {
        int nResultado=0;
        
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteTelefonoValida(?,?,?)}");              
                                   
            oCst.setInt(1, ent.getCod_tel_in());
            oCst.setInt(2, ent.getCod_cli_in());
            oCst.registerOutParameter(3, java.sql.Types.INTEGER);
            
            oCst.execute();
            nResultado=oCst.getInt(3);
            
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nResultado;
    }    
}
